package com.software.levelupandroid.taskShow;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.taskShow.adapter.TaskShowAdapter;
import com.software.levelupandroid.taskShow.viewModel.TaskShowViewModel;
import com.software.levelupandroid.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class TaskShowActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView tvTaskShowTitle;
    private RecyclerView rvTaskShow;
    private TaskShowAdapter taskShowAdapter;
    private TaskShowViewModel taskShowViewModel;
    private List<Task> tasks = new ArrayList<>();
    private int status = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_show);

        initView();

        Intent intent = getIntent();
        if (intent != null){
            status = intent.getIntExtra("status", 0);
        }
        if (status == 1){
            tvTaskShowTitle.setText("已完成任务展示");
        } else if (status == 0) {
            tvTaskShowTitle.setText("进行中任务展示");
        } else if (status == -1) {
            tvTaskShowTitle.setText("未完成任务展示");
        }

        initViewModel();

        taskShowViewModel.getTaskListByStatus(this , status);

        initToolbar();

        recyclerManager();

        observeLiveData();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void observeLiveData() {
        taskShowViewModel.getTaskListLiveData().observe(this, new Observer<Resource<List<Task>>>() {
            @Override
            public void onChanged(Resource<List<Task>> listResource) {
                if (listResource != null){
                    if (listResource.isSuccess()){
                        taskShowAdapter.updateTaskList(listResource.getData());
                    }else if (listResource.isFailure()){
                        ToastUtil.showToastOnUI(TaskShowActivity.this, listResource.getErrorMessage());
                    } else if (listResource.isLoading()) {

                    }
                }
            }
        });
    }

    private void initViewModel() {
        ViewModelProvider viewModelProvider = new ViewModelProvider(this);
        taskShowViewModel = viewModelProvider.get(TaskShowViewModel.class);
    }

    private void initToolbar() {
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayShowTitleEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_task_show_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_taskShow_finish:
                status = 1;
                tvTaskShowTitle.setText("已完成任务展示");
                taskShowViewModel.getTaskListByStatus(this , status);
                return true;
            case R.id.menu_taskShow_failure:
                status = -1;
                tvTaskShowTitle.setText("未完成任务展示");
                taskShowViewModel.getTaskListByStatus(this , status);
                return true;
            case R.id.menu_taskShow_ongoing:
                status = 0;
                tvTaskShowTitle.setText("进行中任务展示");
                taskShowViewModel.getTaskListByStatus(this , status);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }

    private void recyclerManager() {
        taskShowAdapter = new TaskShowAdapter(this, tasks);
        rvTaskShow.setLayoutManager(new LinearLayoutManager(this));
        rvTaskShow.setAdapter(taskShowAdapter);
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar_taskShow);
        tvTaskShowTitle = findViewById(R.id.tv_taskShow_title);
        rvTaskShow = findViewById(R.id.rv_taskShow);
    }
}